package com.example.demo;



import java.io.File;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.example.demo.entity.FoodType;

//jsoup爬虫测试
public class test {
	public static void main(String[] args)throws Exception {
		Connection conn=Jsoup.connect("https://home.meishichina.com/recipe/recai/");
		Document doc=conn.get();
//		System.out.println(doc.html());
		Elements els=doc.select(".nav_wrap2 li");
		for(int i=1;i<7;i++) {
			Element el=els.get(i);
			FoodType type= new FoodType();
			type.setName(el.selectFirst("a").text());
			//service.insert(type);   //类别名称   添加类型，获取id
			int typeid=type.getId();
			
			
			String url=el.selectFirst("a").attr("href");
			getpage(url,1,4,typeid);
		}
	}
	public static void getpage(String url,int page,int max,int typeid)throws Exception {
		Connection conn=Jsoup.connect(url+"/page/"+page+"/");
		Document doc=conn.get();
		Elements els=doc.select(".ui_newlist_1  li");
		for(int i=1;i<els.size();i++) {
			Element el=els.get(i);
			System.out.println(el.selectFirst(".pic img").attr("data-src"));//picurl
			System.out.println(el.selectFirst(".detail h2 a").text());//name
			//添加菜  
		}
		if(page<max)
		getpage(url,++page,max,typeid);
		
	}

}
